
jQuery(document).ready(function ($) {

    // Delete text in inputs on focus
    jQuery('input[type=text]').focus(function () {
        if (this.value == this.title) {
            jQuery(this).val("");
        }
    }).blur(function () {
        if (this.value == "") {
            jQuery(this).val(this.title);
        }
    });

    // Pressing enter on searchbar inputs submits the search
    jQuery('#searchbar :input').bind('keypress', function (e) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == 13) {
            e.preventDefault();
            var submit = jQuery(this).parents('#searchbar').find('.searchJobs');
            $(submit).click();
        }
    });

    // Bottom carousel functions
    var slider = jQuery(".royalSlider").royalSlider({
        keyboardNavEnabled: true,
        //slidesSpacing: 22,
        loop: true,
        transitionSpeed: 800,
        numImagesToPreload: 8,
        fadeinLoadedSlide: false,
        navigateByClick: false
    }).data('royalSlider');

    //mop-carousel

    jQuery('#leftArrow').click(function () { slider.prev(); });
    jQuery('#rightArrow').click(function () { slider.next(); });


    // Homepage only functions
    if (jQuery('.home-container').length > 0) {

        var openButton = '#open-drawer';
        var closeButton = '#close-drawer';
        var slideTime = 700;
        var fadeSpeed = 200;
        var easing = 'easeOutQuart';

        // Show 'At Work' button
        jQuery(openButton).fadeToggle().animate({ bottom: "-37" }, fadeSpeed, easing);

        // Move the button after a delay
        //setTimeout(function () { pulseButton(openButton, easing) }, 2500);

        // Homepage 'At Play' slide drawer functions
        jQuery(openButton).click(
        function () {
            jQuery('#drawer-container').animate({ top: "0" }, slideTime, easing, function () {
                jQuery(closeButton).fadeToggle();
            });
            jQuery(this).fadeToggle().animate({ bottom: "0" }, fadeSpeed);
        });
        jQuery(closeButton).click(
        function () {
            jQuery('#drawer-container').animate({ top: "-516" }, slideTime, easing);
            if (jQuery('#open-drawer:hidden').length > 0) {
                jQuery(closeButton).fadeToggle();
                jQuery(openButton).fadeToggle(fadeSpeed).animate({ bottom: "-37" }, fadeSpeed, easing);
            }
        });

        // If clicking outside of the drawer, close it
        jQuery(document).mouseup(function (e) {
            var container = jQuery("#drawer-container, .carousel, .ac_results, .navigationlinkLi");
            if (container.has(e.target).length === 0)
                jQuery(closeButton).click();
        });
    }

    // Left tabs
    if (jQuery('.leftNav').length > 0) {
        jQuery('.leftNav li:first').click();
    }
});

function pulseButton(id, ease) {
    jQuery(id).animate({ bottom: "-43", paddingTop: "12" }, 80, ease,
    function () {
        jQuery(this).delay(400).animate({ bottom: "-37", paddingTop: "6" }, 80, ease);
    });
}

function expandToggle(ele) {
    jQuery(ele).parents('.careerExpander').toggleClass('open');    
    jQuery(ele).siblings('.careerInfo').slideToggle(500, 'easeOutQuart');
}

function expandAll() {
    jQuery('.careerExpander').addClass('open');
    jQuery('.careerInfo').slideDown(500, 'easeOutQuart');
}

function collapseAll() {
    jQuery('.careerExpander').removeClass('open');
    jQuery('.careerInfo').slideUp(500, 'easeOutQuart');
}

function leftNavClick(ele) {
    var index = jQuery('.leftNav li').removeClass('current').index(ele);
    jQuery(ele).addClass('current');
    jQuery('.centerCopy').hide().eq(index).show();
}
